//
//  CZEmoticonKeyBoard.swift
//  Blog
//
//  Created by whitcast on 16/7/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// 重用标记
private let CZEmoticonCollectionViewCellIdentifier = "CZEmoticonCollectionViewCellIdentifier"

class CZEmoticonKeyBoard: UIView {
    private lazy var toolBar: CZEmoticonToolBar = {
        let view = CZEmoticonToolBar(frame: CGRectZero)
        return view
    }()
    
    // 表情视图
    private lazy var emoticonCollectionView: UICollectionView = {
        // 布局方式
        let flowLayout = UICollectionViewFlowLayout()
        //  设置水平方向滚动
        flowLayout.scrollDirection = .Horizontal
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        // 去掉水平和垂直方向滚动条
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        // 开启分页模式
        view.pagingEnabled = true
        // 去掉弹簧效果
        view.bounces = false
        
        view.backgroundColor = self.backgroundColor
        // 注册cell
        view.registerClass(CZEmoticonCollectionViewCell.self, forCellWithReuseIdentifier: CZEmoticonCollectionViewCellIdentifier)
        // 设置数据源代理
        view.dataSource = self
        return view

    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 重写layoutSubviews，获取子控件大小
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 获取表情视图布局方式
        let flowLayout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        // 每项的大小等于我们表情视图的大小
        flowLayout.itemSize = emoticonCollectionView.size
        // 设置水平间距
        flowLayout.minimumInteritemSpacing = 0
        // 设置垂直间距
        flowLayout.minimumLineSpacing = 0
    }
    
    private func setupUI() {
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(emoticonCollectionView)
        addSubview(toolBar)
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self.toolBar.snp_top)
        }
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.trailing.equalTo(self)
            
            make.height.equalTo(30)

        }
        
        //  设置闭包
        toolBar.selectedToolBarButtonClosure = { [weak self] (type: CZEmoticonToolBarButtonType) in
            switch type {
            case .Normal:
                print("normal")
            case .Emoji:
                print("emoji")
            case .Lxh:
                print("lxh")
            }
            
        }

        
    }
   
}

extension CZEmoticonKeyBoard:UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return CZEmoticonTools.shareTools.allEmoticonArray.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return CZEmoticonTools.shareTools.allEmoticonArray[section].count
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZEmoticonCollectionViewCellIdentifier, forIndexPath: indexPath) as! CZEmoticonCollectionViewCell
        cell.indexPath = indexPath
        // 设置模型
        cell.emoticons = CZEmoticonTools.shareTools.allEmoticonArray[indexPath.section][indexPath.item]
        return cell
    }
}